<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>使用XYZ的方式加载OpenStreetMap 2</title>
    <link rel="stylesheet" href="../v6.4.3/css/ol.css">
    <style type="text/css">
    html, body {
      height: 100%;
    }
    </style>
  </head>
  <body>
    <div id="map" style="height: 100%;"></div>

    <script src="../v6.4.3/build/ol.js"></script>
    <script>
      var proj_3857 = new ol.proj.get('EPSG:3857')
      // console.warn(proj_3857)
      var proj_3857Extent = proj_3857.getExtent()
      var resolutions3857 = [156543.03392804097, 78271.51696402048, 39135.75848201024,
        19567.87924100512, 9783.93962050256, 4891.96981025128, 2445.98490512564, 1222.99245256282,
        611.49622628141, 305.748113140705, 152.8740565703525, 76.43702828517625, 38.21851414258813,
        19.109257071294063, 9.554628535647032, 4.777314267823516, 2.388657133911758, 1.194328566955879,
        0.5971642834779395
      ]
      var resolutionsBaidu = []
      //计算百度使用的分辨率
      for(var i = 0; i <= 18; i++) {
        resolutionsBaidu[i] = Math.pow(2, 18 - i)
      }
      console.error(proj_3857Extent)
      console.error(ol.extent.getTopLeft(proj_3857Extent))
      var tileGrid = new ol.tilegrid.TileGrid({
        extent: proj_3857Extent,
        origin: ol.extent.getTopLeft(proj_3857Extent),
        tileSize: [256, 256],
        resolutions: resolutions3857
      })
      
      var mapLayer = new ol.layer.Tile({
        source: new ol.source.XYZ({
          // url: 'http://{a-c}.tile.openstreetmap.org/{z}/{x}/{y}.png'  //XYZ方式加载OpenStreetMap
          url: 'http://webst0{1-4}.is.autonavi.com/appmaptile?lang=zh_cn&size=1&scale=1&style=7&x={x}&y={y}&z={z}' //XYZ方式加载高德地图
        }),
        tileGrid: tileGrid,
        projection: proj_3857
      })

      // console.log(mapLayer)
      console.log(tileGrid)

      var map = new ol.Map({
        target: 'map',
        layers: [
          mapLayer
        ],
        view: new ol.View({
          center: ol.proj.fromLonLat([104.06, 30.67]),
          zoom: 11
        })
      })
    </script>
  </body>
</html>
